package com.heaven.map.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heaven.map.mapper.TCityMapper;
import com.heaven.map.mapper.TDistrictMapper;
import com.heaven.map.mapper.TProvinceMapper;
import com.heaven.map.pojo.po.TCity;
import com.heaven.map.pojo.po.TProvince;
import com.heaven.map.pojo.vo.MapVO;
import com.heaven.map.pojo.vo.ResponseInfo;
import com.heaven.map.pojo.po.TUser;
import com.heaven.map.mapper.TUserMapper;
import com.heaven.map.pojo.vo.TableVO;
import com.heaven.map.service.ITUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heaven.map.utils.CodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Heaven
 * @since 2021-10-03
 */
@Service
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements ITUserService {

    private static final Logger log1 = LoggerFactory.getLogger(TUserServiceImpl.class);
    @Autowired
    private TUserMapper tUserMapper;
    @Autowired
    private TCityMapper tCityMapper;
    @Autowired
    private TProvinceMapper tProvinceMapper;

    @Override
    public ResponseInfo listCount() {
        Integer allUsers = tUserMapper.selectCount(null);
        Integer driving = tUserMapper.selectCount(new QueryWrapper<TUser>().eq("driving", true));
        Integer danger_drive = tUserMapper.selectCount(new QueryWrapper<TUser>().eq("danger_drive", true));
        Integer normal_drive = driving - danger_drive;
        Integer[] allNumber = {allUsers, driving, danger_drive, normal_drive};
        log1.debug("allUser:{},driving:{},danger_drive:{},normal_drive:{}", allUsers, driving, danger_drive, normal_drive);
        return new ResponseInfo(allNumber, "成功", CodeEnum.OK.getCode());
    }

    @Override
    public ResponseInfo addUser(TUser user) {
        return null;
    }

    @Override
    public ResponseInfo deleteUser(TUser user) {
        return null;
    }

    @Override
    public ResponseInfo listUsers(Integer currentPage, Integer pageSize) {
        IPage<TUser> iPage = new Page<>(currentPage, pageSize);
        tUserMapper.selectPage(iPage, null);
        List<TableVO> tableVOS = new ArrayList<>();
        log1.debug("iPageCurrent:{},iPageRecords:{},iPageGetPages:{},iPageGetSize:{},iPageGetTotal:{}", iPage.getCurrent(), iPage.getRecords(), iPage.getPages(), iPage.getSize(), iPage.getTotal());
        for (TUser tUser : iPage.getRecords()) {
            String cityName = tCityMapper.selectOne(new QueryWrapper<TCity>().eq("city_id", tUser.getCityId())).getCityName();
            String provinceName = tProvinceMapper.selectOne(new QueryWrapper<TProvince>().eq("province_id", tUser.getProvinceId())).getProvinceName();
            String details = provinceName + cityName + tUser.getDetailAddress();
            String lon_lat = tUser.getLon() + ',' + tUser.getLat();
            TableVO tableVO = null;
            if (tUser.getDriving() == false) {
                tableVO = new TableVO(tUser.getId(), tUser.getIdentityCard(), tUser.getContactTelephone(), tUser.getName(), details, tUser.getDangerDrive(), tUser.getDriving(), null);
            } else {
                tableVO = new TableVO(tUser.getId(), tUser.getIdentityCard(), tUser.getContactTelephone(), tUser.getName(), details, tUser.getDangerDrive(), tUser.getDriving(), lon_lat);
            }
            tableVOS.add(tableVO);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("data", tableVOS);
        map.put("pageSize", iPage.getPages());
        return new ResponseInfo(map, "success", CodeEnum.OK.getCode());

    }

    @Override
    public ResponseInfo ModifyDriveState(Integer id) {
        TUser tUser = new TUser();
        tUser.setDangerDrive(!(tUserMapper.selectById(id).getDangerDrive()));
        tUser.setId(id);
        int i = tUserMapper.updateById(tUser);
        if (i != 1) {
            return new ResponseInfo(null, "修改失败", 500);
        } else
            return new ResponseInfo(null, "修改成功", 200);
    }

    @Override
    public ResponseInfo listDrivingUser(Integer dangerDriving) {
        List<TUser> users = null;
        Boolean isDangerDriving = false;
        if (dangerDriving == 0) {
            users = tUserMapper.selectList(new QueryWrapper<TUser>().eq("driving", true));
        } else if (dangerDriving == 1) {
            isDangerDriving = true;
        } else if (dangerDriving == 2) {
            isDangerDriving = false;
        }
        if (dangerDriving != 0)
            users = tUserMapper.selectList(new QueryWrapper<TUser>().eq("driving", true).eq("danger_drive", isDangerDriving));

        List<MapVO> userMap = new ArrayList<>();
        for (TUser user : users) {
            MapVO mapVO = new MapVO(user.getLon(), user.getLat(), user.getName(), user.getIdentityCard(), user.getDangerDrive());
            userMap.add(mapVO);
        }
        return new ResponseInfo(userMap, "success", 200);
    }

    @Override
    public ResponseInfo deleteUser(Integer id) {
        int i = tUserMapper.deleteById(id);
        if (i == 1) {
            return new ResponseInfo(null, "删除成功", 200);
        } else {
            return new ResponseInfo(null, "删除失败", 500);
        }
    }
}
